<?php

namespace app\Api\controller;

use app\common\business\SCategory as SCategoryBus;
use app\common\lib\Check;
use app\common\lib\Str;
use app\common\model\mysql\SCategory;

class Category extends ApiBase
{
    //首页商品推荐类别
    public function pageCategoryList(){
        $typeData = [
            ['type_id'=>1, 'type_name'=>'热卖单品'],
            ['type_id'=>2, 'type_name'=>'促销单品'],
            ['type_id'=>3, 'type_name'=>'精品推荐'],
            ['type_id'=>4, 'type_name'=>'首发新品'],
            ['type_id'=>5, 'type_name'=>'优品推荐'],
        ];
        return show(config("status.success"),"首页商品推荐类别",$typeData);
    }

    // 商品一级分类列表
    public function firstCategoryList(){
        $where[] = ["parent_id", "=", 0];
        $where[] = ["status", "=", '0'];
        $where[] = ["del_flag", "=", '0'];
        try {
            $field = "category_id,category_name,category_img";
            $categoryData = (new SCategoryBus())->getNormalCategory($where,$field);
            foreach ($categoryData as &$category){
                $category['category_img'] = (new Check())->checkUrl($category['category_img']);
            }
        }catch (\Exception $e){
            $categoryData = [];
        }
        return show(config("status.success"),"商品一级分类列表",$categoryData);
    }

    // 商品二级分类列表
    public function secondCategoryList(){
        $category_id = $this->request->param("category_id","","trim");
        if(empty($category_id)){
            return show(config("status.error"),"一级分类id不能为空");
        }
        $where[] = ["parent_id", "=", $category_id];
        $where[] = ["status", "=", '0'];
        $where[] = ["del_flag", "=", '0'];
        try {
            $field = "category_id,category_name,category_img";
            $categoryData = (new SCategoryBus())->getNormalCategory($where,$field);
            foreach ($categoryData as &$category){
                $category['category_img'] = (new Check())->checkUrl($category['category_img']);
            }
        }catch (\Exception $e){
            $categoryData = [];
        }
        return show(config("status.success"),"商品二级分类列表",$categoryData);
    }

    // 分类无限极下拉结构
    public function categoryTree(){
        $categoryData = (new SCategory())->getCategoryTreeChildrenFrontEnd();
        foreach ($categoryData as &$category){
            $category['category_img'] = (new Check())->checkUrl($category['category_img']);
            foreach ($category['children'] as &$categoryChild){
                $categoryChild['category_img'] = (new Check())->checkUrl($categoryChild['category_img']);
            }
        }
        return show(config("status.success"),"分类无限极下拉结构",$categoryData);
    }
}
